import datetime

from greatwall.models import Card, Stage, CardComments

class CardOperations(object):

    def __init__(self):
        pass

    def get_card_by_id(self, card_id):
        card = Card.objects.get(id=card_id)
        return card

    def get_object_by_id(self, table_name, id):
        stage_obj = table_name.objects.get(id=id)


    def get_wall_by_stage_id(self, stage_id):
        stage_obj = Stage.objects.get(id=stage_id)
        return stage_obj.fk_wall_id

    def create_card(self, title='', stage=None, start_dt='',
                    end_dt='', devs='', desc='', card_color=''):
        card = Card()
        card.title = title
        card.fk_stage = stage
        card.date_created = datetime.datetime.today()
        #card.start_date = start_dt
        #card.end_date = end_dt
        card.developers = devs
        card.description = desc
        card.card_color = card_color
        card.save()
        return card

    def create_dummy_card(self, stage=None, title=''):
        tendays = datetime.timedelta(days=10)
        today = datetime.datetime.today()
        card = self.create_card(title=title, stage=stage,
                                start_dt=today, end_dt=today+tendays,
                                devs='Jayesh/Arun', desc='this is a dummy card')

    def delete_card(self, card_id):
        card_obj = self.get_card_by_id(card_id)
        wall_id = self.get_wall_by_stage_id(card_obj.fk_stage_id)
        card_obj.delete()
        return str(wall_id)

    def save_edit_card(self, card_id, card_list):
        card_obj = self.get_card_by_id(card_id)
        card_obj.title = card_list[0]
        #card_obj.start_date = datetime.datetime(card_list[1])
        #card_obj.end_date = card_list[2]
        card_obj.developers = card_list[3]
        card_obj.points = card_list[4]
        card_obj.description = card_list[5]
        card_obj.card_color = card_list[6]
        card_obj.save()

    def get_card_comments(self, card_id):

        try:
            card_obj = self.get_card_by_id(card_id)
            comments_list = CardComments.objects.filter(fk_card = card_obj)
            #comments_list = CardComments.objects.all()
        except:
            comments_list = ""

        return comments_list

    def add_comments(self, card_id, comments):
        card_comments = CardComments()
        card_comments.comment = comments
        card_comments.fk_card = self.get_card_by_id(card_id)
        card_comments.date_created = datetime.datetime.today()
        card_comments.save()

    def toogle_card(self, card):
        toogle_dict = {True: False, False: True}
        card.is_open = toogle_dict[card.is_open]
        card.save()

    def save_title(self, card_id, title):
	card_obj = self.get_card_by_id(card_id)
	card_obj.title = title
	card_obj.save()